let BASE_URL="http://localhost:8099"
const http = {
    get: function (url, callback) {
        let xhr = new XMLHttpRequest();
        xhr.open('get',BASE_URL+url);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 & xhr.status === 200) {
                let res = xhr.responseText;
                res = JSON.parse(res)
                callback(res)
            }
        }
        xhr.send()
    },
    post: function (url, name1, callback) {
        let xhr = new XMLHttpRequest();
        xhr.open('post',BASE_URL+url)
        xhr.setRequestHeader("content-type", "application/json");
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                let res = xhr.responseText;
                res = JSON.parse(xhr.responseText);
                callback(res)
            }
        }
        let str = JSON.stringify(name1)
        xhr.send(str)
    },
    ajax: function (obj) {
        if (obj.type === 'post') {
            let xhr = new XMLHttpRequest();
            xhr.open('post', obj.url);
            xhr.setRequestHeader('Content-Type', 'application/json');
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    let res = JSON.parse(xhr.responseText);
                    obj.success(res);
                }
            }
            xhr.send(JSON.stringify(obj.data));
        }
        if (obj.type === 'get') {
            let xhr = new XMLHttpRequest();
            let str = '';
            for (key in obj.data) {
                str += `&${key}=${obj.data[key]}`;
            }
            str = str.substr(1);
            let url = obj.url + '?' + str;
            xhr.open('get', url);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    let res = xhr.responseText;
                    res = JSON.parse(res);
                    obj.success(res);
                }
            }
            xhr.send()
        }

    },
    flile:function(url, fdKey, fdValue, success) {
        const xhr = new XMLHttpRequest();
      
        const fd = new FormData();
        fd.append(fdKey, fdValue);
      
        xhr.open('POST', url);
        xhr.onreadystatechange = function() {
          if (xhr.readyState === 4 && xhr.status === 200) {
            const resData = JSON.parse(xhr.responseText)
            success(resData)
          }
        }
        xhr.send(fd);
      }

}

/**
   * 封装调用本地时间函数
   *@forGetTime
   *@date  传入参数为new Date()或者'1990-01-02'格式
   * 作者：kasswoler
   * 时间：2022年1月2日
   * */
   function forGetTime(date,choose=0){
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    let hour = date.getHours();
    let min = date.getMinutes();
    let sec = date.getSeconds();
    if(choose==0){
      let str = `${year}-${month > 10 ? "" : 0}${month}-${
        day > 10 ? "" : 0
      }${day} ${hour > 10 ? "" : 0}${hour}:${min > 10 ? "" : 0}${min}:${
        sec > 10 ? "" : 0
      }${sec}`;
      return str;
    }else if(choose===1){
      let str = `${year}-${month > 10 ? "" : 0}${month}-${
        day > 10 ? "" : 0
      }${day} `;
      return str;
    }
    
  }


window.http = window.http = http